<template>
  <div>
    <div :class="['step-item', {last: isLast, current: isActive}]">
      <slot name="title">
        <span>{{title}}</span>
      </slot>
    </div>
  </div>
</template>

<script>
export default {
  name: 'TomsStep',
  props: {
    title: String
  },
  data() {
    return {
      index: -1
    }
  },
  computed: {
    isLast() {
      const parent = this.$parent
      return parent.steps[parent.steps.length - 1] === this
    },
    isActive() {
      const parent = this.$parent
      if (this.isLast && parent.active > this.index) {
        return true
      }
      return this.index + 1 === parent.active
    },
    stepsCount() {
      return this.$parent.steps.length
    }
  },
  beforeCreate() {
    this.$parent.steps.push(this)
  },
  beforeDestroy() {
    const steps = this.$parent.steps
    const index = steps.indexOf(this)
    if (index >= 0) {
      steps.splice(index, 1)
    }
  },
  name: 'TomsSteps'
}
</script>

<style lang="scss" scoped>
$activeColor: #3688fc;

.steps {
  display: flex;
  position: relative;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  @apply pb-5 mb-4 mt-4;

  &:before {
    content: '';
    display: block;
    position: absolute;
    width: 100%;
    height: 0.2em;
    background-color: #eef2f7;
  }

  .steps-content {
    display: flex;
    position: relative;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    .step-item {
      display: block;
      height: 8px;
      width: 8px;
      margin: 0 2em;
      position: relative;
      box-sizing: content-box;
      color: $activeColor;
      background-color: currentColor;
      border-radius: 50%;
      border: 0.25em solid #fafbfe;
      z-index: 5;
      &.current {
        color: $activeColor;
        &:before {
          content: '';
          display: block;
          position: absolute;
          top: 47.5%;
          left: 51%;
          padding: 1em;
          background-color: currentColor;
          border-radius: 50%;
          opacity: 0;
          /* z-index: -1; */
          animation-name: animation-steps-current;
          animation-duration: 2s;
          animation-iteration-count: infinite;
          animation-timing-function: ease-out;
        }
        span {
          color: $activeColor;
        }
      }
      &.last {
        margin-right: 0;
        color: #42d29d;
      }
      &:first-child {
        margin-left: 0;
      }
      span {
        position: absolute;
        top: calc(100% + 1em);
        left: 50%;
        -webkit-transform: translateX(-50%);
        transform: translateX(-50%);
        white-space: nowrap;
        color: #adb5bd;
      }
    }
  }
}
@keyframes animation-steps-current {
  from {
    -webkit-transform: translate(-50%, -50%) scale(0);
    transform: translate(-50%, -50%) scale(0);
    opacity: 1;
  }

  to {
    -webkit-transform: translate(-50%, -50%) scale(1);
    transform: translate(-50%, -50%) scale(1);
    opacity: 0;
  }
}
</style>
